using System.Collections;
using System.Collections.Generic;
using UnityEngine;

static public class CharacterActions
{
    public static WaitForSeconds waitSeconds = new WaitForSeconds(0.2f);

    public static IEnumerator TakeDefenceForFriend(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.playerDataHolder)
                    {
                        item.TakeDefence(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        while (FightController.Instance.playerDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        }
                        FightController.Instance.playerDataHolder[order].TakeDefence(data.actionNum, FightController.Instance.playerDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeDefence(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeDefence(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.playerDataHolder.Count)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder++].TakeDefence(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
    }

    public static IEnumerator TakeDefenceForEnemy(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.enemyDataHolder)
                    {
                        item.TakeDefence(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        while (FightController.Instance.enemyDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        }
                        FightController.Instance.enemyDataHolder[order].TakeDefence(data.actionNum, FightController.Instance.enemyDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeDefence(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeDefence(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.enemyDataHolder.Count)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++].TakeDefence(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
    }

    public static IEnumerator AttackEnemy(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.enemyDataHolder)
                    {
                        item.TakeDamage(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        while(FightController.Instance.enemyDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        }
                        FightController.Instance.enemyDataHolder[order].TakeDamage(data.actionNum, FightController.Instance.enemyDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeDamage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeDamage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.enemyDataHolder.Count)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++].TakeDamage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
        FightController.Instance.isCheckPlayerWin = true;
    }

    public static IEnumerator AttackFriend(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.playerDataHolder)
                    {
                        item.TakeDamage(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        while (FightController.Instance.playerDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        }
                        FightController.Instance.playerDataHolder[order].TakeDamage(data.actionNum, FightController.Instance.playerDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeDamage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeDamage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.playerDataHolder.Count)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder++].TakeDamage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
        FightController.Instance.isCheckPlayerWin = true;
    }

    public static IEnumerator TakeCourgeForFriend(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.playerDataHolder)
                    {
                        item.TakeCourage(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        while (FightController.Instance.playerDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.playerDataHolder.Count);
                        }
                        FightController.Instance.playerDataHolder[order].TakeCourage(data.actionNum, FightController.Instance.playerDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeCourage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.playerDataHolder[data.chooseCharacterOrder].TakeCourage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.playerDataHolder.Count)
                    {
                        FightController.Instance.playerDataHolder[data.chooseCharacterOrder++].TakeCourage(data.actionNum, FightController.Instance.playerDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
    }

    public static IEnumerator TakeCourgeForEnemy(CharacterActionData data)
    {
        TurnController.Instance.canEndPlayerTurn = false;
        switch (data.actionRange)
        {
            case -1://分裂
                Debug.LogWarning("分裂还没写");
                break;
            case 0://全体
                for (int i = 0; i < data.actionCount; i++)
                {
                    foreach (var item in FightController.Instance.enemyDataHolder)
                    {
                        item.TakeCourage(data.actionNum, item);
                    }
                    yield return waitSeconds;
                }
                break;
            case 1://单体
                if (data.chooseCharacterOrder == -1)
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        int order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        while (FightController.Instance.enemyDataHolder[order].characterData.isDead())
                        {
                            order = RandomEngine.Instance.GetRandCombatRewards(0, FightController.Instance.enemyDataHolder.Count);
                        }
                        FightController.Instance.enemyDataHolder[order].TakeCourage(data.actionNum, FightController.Instance.enemyDataHolder[order]);
                        yield return waitSeconds;
                    }
                }
                else
                {
                    for (int i = 0; i < data.actionCount; i++)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeCourage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                        yield return waitSeconds;
                    }
                }
                break;
            case 2://前两个
                for (int i = 0; i < data.actionCount; i++)
                {
                    FightController.Instance.enemyDataHolder[data.chooseCharacterOrder].TakeCourage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder]);
                    if (data.chooseCharacterOrder++ <= FightController.Instance.enemyDataHolder.Count)
                    {
                        FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++].TakeCourage(data.actionNum, FightController.Instance.enemyDataHolder[data.chooseCharacterOrder++]);
                    }
                    yield return waitSeconds;
                }
                break;
        }
        TurnController.Instance.canEndPlayerTurn = true;
    }

    
}
